# Balance tables ---------------------------------------------------------------


# Videos ---------------------------------------------------------------


f_balance_videos_placebo <- df %>%
  filter(video_type %in% c("control", "placebo")) %>%
  feols_custom(
    as.formula(paste0(
      "video_type_placebo ~ ", fml_sum(demo_vars)
    )),
    data = .
  ) %>%
  f_test()

f_balance_videos_treat <- df %>%
  filter(video_type %in% c("control", "treatment")) %>%
  feols_custom(
    as.formula(paste0(
      "video_type_treatment ~ ", fml_sum(demo_vars)
    )),
    data = .
  ) %>%
  f_test()


# 1. Videos
df %>%
  filter(phase == "phase_2") %>%
  left_join(group_relations_ind, by = c("ind_id", "group_id")) %>%
  select(video_type, all_of(demo_vars)) %>%

  RCT::balance_table("video_type") %>%

  rename_with(
    ~ .x %>% str_replace("Media_", "") %>%
      str_replace("p_value", "p_val_") %>%
      str_replace("variablesplacebo", "variable") %>%
      str_replace("controlplacebo", "mean_control") %>%
      str_replace("trat", "mean_")
  ) %>%
  mutate(across(starts_with("p_val"),
                list(sig = ~case_when(.x <= 0.01~ "***",
                                      .x <= 0.05 ~ "**",
                                      .x <= 0.1 ~ "*",
                                      TRUE ~ "")))) %>%

  mutate(demo_var = fct_relevel(factor(variable), names(demo_vars))) %>%
  arrange(demo_var) %>%
  select(Variable = variable, `Mean Control video` = mean_control, `Mean Rights messaging\nvideo` = mean_placebo, `Mean Legal rights video` = mean_treatment,
         `p-value (2) - (1)` = p_val_placebo, `\n` = p_val_placebo_sig, `p-value (3) - (1)` = p_val_treatment, `\n\n` = p_val_treatment_sig) %>%

  add_row(
    "Variable" = "F-test: statistic",
    `p-value (2) - (1)` = f_balance_videos_placebo$stat,
    `p-value (3) - (1)` = f_balance_videos_treat$stat
  ) %>%
  add_row(
    "Variable" = "F-test: p-value",
    `p-value (2) - (1)` = f_balance_videos_placebo$p,
    `p-value (3) - (1)` = f_balance_videos_treat$p
  ) %>%

  kable_balance() %>%

  write_lines("outputs/tables/balance_videos.tex")

# Discussion type - pooled ---------------------------------------------------------------

# p-value
f_balance_main <- df %>%
  filter(discuss_type %in% c("discussion_full", "control")) %>%
  feols_custom(
    as.formula(paste0(
      "discussion_full ~ ", fml_sum(demo_vars)
    )),
    data = .
  ) %>%
  f_test()


tab_balance_main <- df %>%
  filter(discuss_type %in% c("discussion_full", "control")) %>%
  select(discuss_type_label, all_of(demo_vars)) %>%
  RCT::balance_table("discuss_type_label") %>%
  rename_with(
    ~ .x %>% str_replace("Media_", "") %>%
      str_replace("p_value", "p_val_") %>%
      str_replace("variablesplacebo", "variable") %>%
      str_replace("controlplacebo", "mean_control") %>%
      str_replace("trat", "mean_") %>%
      str_replace("_?3-person discussion", "")
  ) %>%
  mutate(across(starts_with("p_val"),
                list(sig = ~case_when(.x <= 0.01~ "***",
                                      .x <= 0.05 ~ "**",
                                      .x <= 0.1 ~ "*",
                                      TRUE ~ "")))) %>%
  mutate(demo_var = fct_relevel(factor(variables), names(demo_vars))) %>%
  arrange(demo_var) %>%
  select(Variable = variables, `Mean No discussion (private)` = control, `Mean 3-person discussion` = mean,
         `p-value (1)=(2)` = p_val, `\n` = p_val_sig) %>%
  add_row(
    "Variable" = "F-test: statistic",
    `p-value (1)=(2)` = f_balance_main$stat
  ) %>%
  add_row(
    "Variable" = "F-test: p-value",
    `p-value (1)=(2)` = f_balance_main$p
  ) %>%
  kable_balance() %>%
  write_lines("outputs/tables/balance_pooled.tex")


# Phase 2 ---------------------------------------------------------------


f_balance_public <- df %>%
  left_join(group_relations_ind, by = c("ind_id", "group_id")) %>%
  filter(phase == "phase_2", discuss_type %in% c("control", "choosing_only")) %>%
  feols_custom(as.formula(paste0("choosing_only ~ ", fml_sum(demo_vars))), data = .) %>%
  f_test()

f_balance_discussion_pair <- df %>%
  left_join(group_relations_ind, by = c("ind_id", "group_id")) %>%
  filter(phase == "phase_2", discuss_type %in% c("control", "discussion_pair")) %>%
  feols_custom(as.formula(paste0("discussion_pair ~ ", fml_sum(demo_vars))), data = .) %>%
  f_test()

f_balance_discussion_full <- df %>%
  left_join(group_relations_ind, by = c("ind_id", "group_id")) %>%
  filter(phase == "phase_2", discuss_type %in% c("control", "discussion_full")) %>%
  feols_custom(as.formula(paste0("discussion_full ~ ", fml_sum(demo_vars))), data = .) %>%
  f_test()

df %>%
  filter(phase == "phase_2") %>%
  left_join(group_relations_ind, by = c("ind_id", "group_id")) %>%
  select(discuss_type_label, all_of(demo_vars)) %>%
  mutate(discuss_type_label = fct_relevel(discuss_type_label, "No discussion (private)")) %>%
  RCT::balance_table("discuss_type_label") %>%

  mutate(demo_var = fct_relevel(factor(`variablesNo discussion (public)`), names(demo_vars))) %>%
  arrange(demo_var) %>%
  select(-demo_var) %>%

  set_names(c("Variable", "Mean No discussion (private)", "Mean No discussion (public)", "Mean 2-person discussion", "Mean 3-person discussion",
              "p-value (1)=(2)", "p-value (1)=(3)", "p-value (1)=(4)")) %>%
  mutate(across(starts_with("p-val"),
                list(sig = ~case_when(.x <= 0.01~ "***",
                                      .x <= 0.05 ~ "**",
                                      .x <= 0.1 ~ "*",
                                      TRUE ~ "")))) %>%
  relocate(Variable, starts_with("Mean"), `p-value (1)=(2)`, `\n` = `p-value (1)=(2)_sig`, `p-value (1)=(3)`, `\n\n` = `p-value (1)=(3)_sig`, `p-value (1)=(4)`, `\n\n\n`= `p-value (1)=(4)_sig`) %>%

  add_row(
    "Variable" = "F-test: statistic",
    `p-value (1)=(2)` = f_balance_public$stat,
    `p-value (1)=(3)` = f_balance_discussion_pair$stat,
    `p-value (1)=(4)` = f_balance_discussion_full$stat
  ) %>%
  add_row(
    "Variable" = "F-test: p-value",
    `p-value (1)=(2)` = f_balance_public$p,
    `p-value (1)=(3)` = f_balance_discussion_pair$p,
    `p-value (1)=(4)` = f_balance_discussion_full$p
  ) %>%
  kable_balance() %>%
  write_lines("outputs/tables/balance_phase_2.tex")


# Descriptive stats ---------------------------------------------------------------

# demo_vars

df %>% select(all_of(demo_vars)) %>%
  summarise(across(everything(), mean_na)) %>%
  pivot_longer(everything()) %>%
  print %>%
  mutate(value = case_when(
    str_detect(name, "\\(=1\\)") ~ paste0(round(value, 2) * 100, "%"),
    TRUE ~ as.character(signif(value, 2))
  )) %>%
  write_csv("outputs/tables/sample_characteristics.csv")